(function(){
    //创建R对象
	var js_list = [];
    function R(){
    }
    R.prototype.start = function(config){
        var _this = this;
        _this.routerMap = config.router;                     //路由配置
        _this.mainView = config.view;                        //目标div
        _this.errorTemplateId = config.errorTemplateId;      //错误id
        startRouter();                                      //初始化
        window.onhashchange = function(){                   //监听路由变化
            startRouter();
        };
    };

    R.prototype.stringify = function(routerUrl,paramObj){
        var paramStr='' ,hash;
        for(var i in  paramObj){
            paramStr += i + '=' + encodeURIComponent(paramObj[i]) + '&';
        }
        if(paramStr === ''){
            hash = routerUrl;
        }
        else{
            paramStr = paramStr.substring(0,paramStr.length-1);
            hash = routerUrl + '?' + paramStr;
        }
        return hash;
    };
    R.prototype.parse = function(routerHash){
        var hash = typeof routerHash ==='undefined'?location.hash:routerHash;
        var obj = {
            url:'',
            param: {}
        };
        var param = {},url='';
        var pIndex = hash.indexOf('?');
        if(hash===''){
            return obj;
        }

        if(pIndex>-1){
            url = hash.substring(1,pIndex);
            var paramStr = hash.substring(pIndex+1);
            var paramArr = paramStr.split('&');

            $.each(paramArr,function(i,e){
                var item = e.split('='),
                    key,
                    val;
                key = item[0];
                val = item[1];
                if(key!==''){
                    param[key] = decodeURIComponent(val);
                }


            });
        }
        else{
            url = hash.substring(1);
            param = {};
        }
        return {
            url:url,
            param: param
        };
    };
    function routerAction (routeObj){

        var routerItem = router.routerMap[routeObj.url];

        if(typeof routerItem==='undefined'){
            var defaultsRoute = router.routerMap.defaults;
            routerItem = router.routerMap[defaultsRoute];
            // location.hash = defaultsRoute;
            location.hash = '';
            return false;
        }

        $.ajax({
            type: 'GET',
            url: routerItem.templateUrl,
            dataType: 'html',
            success: function(data, status, xhr){
            	$("body").unbind('click');
//            	$(document).unbind('click');
                $(router.mainView).html(data);

                //console.log($(router.mainView)
                loadScript(routerItem.controller);
                loadCss(routerItem.styles);
            },
            error: function(xhr, errorType, error){
                if($(router.errorTemplateId).length===0){
                    return false;
                }
                var errHtml = $(router.errorTemplateId).html();
                errHtml = errHtml.replace(/{{errStatus}}/,xhr.status);
                errHtml = errHtml.replace(/{{errContent}}/,xhr.responseText);
                $(router.mainView).html(errHtml);
            }
        });
    }

    function startRouter  () {
        var hash = location.hash;               //获取当前URL的锚部分
        var routeObj = router.parse(hash);
        //console.log(routeObj);
        routerAction(routeObj);
    }

    function loadScript(src, callback) {
        if(!src) return;
        
        var script = document.createElement('script'),
            loaded;

        script.setAttribute('src', src);
        script.onreadystatechange = script.onload = function() {
            script.onreadystatechange = null;
            document.documentElement.removeChild(script);
            script = null;
            if (!loaded) {
                if(typeof callback==='function')
                    callback();
            }
            loaded = true;
        };
        
//        js_list.push(src);
        document.documentElement.appendChild(script);
    }

    function loadCss(src,callback){
        if(!src) return;
        $('#view_css').attr('href',src);
//        var head = document.getElementsByTagName('head')[0],
//            cssURL = src,
//            linkTag = document.createElement('link');
//
//         linkTag.href = cssURL;
//         linkTag.setAttribute('rel','stylesheet');
//         linkTag.setAttribute('media','all');
//         linkTag.setAttribute('type','text/css');
//
//         head.appendChild(linkTag);
    }

    window.router = new R();
})();
